﻿using ModbusTCPLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using thinger.DataConvertLib;

namespace ModbusProject
{
    public partial class FrmParamSet : Form
    {
        public FrmParamSet(string initialValue, Variable variable, ModbusTCP modbus)
        {
            InitializeComponent();

            this.lbl_initialValue.Text = initialValue;
            this.lbl_VarType.Text = variable.VarType.ToString();

            this.variable = variable;
            this.modbus = modbus;
        }
        private Variable variable;
        private ModbusTCP modbus;

        private void btn_ok_Click(object sender, EventArgs e)
        {
            bool result = false;
            try
            {
                switch (variable.VarType)
                {
                    case DataType.Short:
                        result = modbus.PreSetSingleRegister((ushort)(variable.Start / 2), Convert.ToInt16(this.txt_SetValue.Text.Trim()));
                        break;
                    case DataType.UShort:
                        result = modbus.PreSetSingleRegister((ushort)(variable.Start / 2), Convert.ToUInt16(this.txt_SetValue.Text.Trim()));
                        break;
                    case DataType.Int:
                        result = modbus.PreSetMultiRegisters((ushort)(variable.Start / 2), ByteArrayLib.GetByteArrayFromInt(Convert.ToInt32(this.txt_SetValue.Text.Trim())));
                        break;
                    case DataType.UInt:
                        result = modbus.PreSetMultiRegisters((ushort)(variable.Start / 2), ByteArrayLib.GetByteArrayFromUInt(Convert.ToUInt32(this.txt_SetValue.Text.Trim())));
                        break;
                    case DataType.Float:
                        result = modbus.PreSetMultiRegisters((ushort)(variable.Start / 2), ByteArrayLib.GetByteArrayFromFloat(Convert.ToSingle(this.txt_SetValue.Text.Trim())));
                        break;
                    case DataType.Double:
                        result = modbus.PreSetMultiRegisters((ushort)(variable.Start / 2), ByteArrayLib.GetByteArrayFromDouble(Convert.ToDouble(this.txt_SetValue.Text.Trim())));
                        break;
                    case DataType.Long:
                        result = modbus.PreSetMultiRegisters((ushort)(variable.Start / 2), ByteArrayLib.GetByteArrayFromLong(Convert.ToInt64(this.txt_SetValue.Text.Trim())));
                        break;
                    case DataType.ULong:
                        result = modbus.PreSetMultiRegisters((ushort)(variable.Start / 2), ByteArrayLib.GetByteArrayFromULong(Convert.ToUInt64(this.txt_SetValue.Text.Trim())));
                        break;
                    case DataType.String:
                        result = modbus.PreSetMultiRegisters((ushort)(variable.Start / 2), ByteArrayLib.GetByteArrayFromSiemensString(this.txt_SetValue.Text.Trim()));
                        break;
                    default:
                        MessageBox.Show("不支持该数据类型写入", "参数设置");
                        return;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show("写入失败，请检查参数数据类型是否正确:"+ex.Message,"参数设置");
                return;
            }
            if(result)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("参数修改失败，请检查通信是否正常", "参数设置");
            }
        }

        private void btn_Cannel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void txt_SetValue_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                this.btn_ok_Click(null,null);
            }
        }
    }
}
